package com.example.lenovo.appstore.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.SearchView;

import com.example.lenovo.appstore.R;
import com.example.lenovo.appstore.adapter.FlowAdapter;
import com.example.lenovo.appstore.adapter.HomeAppListAdapter;
import com.example.lenovo.appstore.utils.StatusBarUtil;
import com.example.lenovo.appstore.view.GalleryItemDecoration;
import com.example.lenovo.appstore.view.MySnapHelper;
import com.example.lenovo.appstore.view.RecyclerViewSpacesItemDecoration;
import com.example.lenovo.model.CategoryListModel;

import java.util.ArrayList;
import java.util.HashMap;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目名称：AppStore
 * 精品推荐tab页
 *
 * @Author 郭迎辉
 * Create by 9:28 on 2019/3/20.
 * Describe:
 */
public class HomeFragment extends Fragment {
    @Bind(R.id.search)
    SearchView searchView;
    @Bind(R.id.list)
    RecyclerView list;
    @Bind(R.id.coverflow_view)
    RecyclerView coverflow_view;
    HomeAppListAdapter adapter;
    FlowAdapter flowadapter;
    private View view;
    public static HomeFragment instance;

    public static HomeFragment getInstance() {
        if (null == instance) {
            instance = new HomeFragment();
        }
        return instance;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayList<CategoryListModel.CategoryList> datas = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            datas.add(new CategoryListModel.CategoryList());
        }

        adapter = new HomeAppListAdapter(getActivity(), datas);
        list.setLayoutManager(new LinearLayoutManager(getActivity()));
        list.setAdapter(adapter);
        list.setHasFixedSize(true);
        list.setNestedScrollingEnabled(false);
        adapter.notifyDataSetChanged();
        flowadapter = new FlowAdapter(getActivity(), datas);
        searchView.setFocusable(false);

        coverflow_view.setAdapter(flowadapter);
        MySnapHelper mMySnapHelper = new MySnapHelper();
        coverflow_view.setOnFlingListener(null);
        mMySnapHelper.attachToRecyclerView(coverflow_view);
        LinearLayoutManager ms = new LinearLayoutManager(getActivity());
        ms.setOrientation(LinearLayoutManager.HORIZONTAL);// 设置 recyclerview 布局方式为横向布局
        coverflow_view.setLayoutManager(ms);
        coverflow_view.setOnFlingListener(null);
        new MySnapHelper().attachToRecyclerView(coverflow_view);


    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (null == view) {
            view = inflater.inflate(R.layout.home_fragment, container, false);
            ButterKnife.bind(this, view);
            initView();
        } else {
            if (null != view.getParent()) {
                ((ViewGroup) view.getParent()).removeAllViews();
            }

        }
        instance = this;
        return view;
    }

    private void initView() {
        // 隐藏软键盘
        getActivity().getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        StatusBarUtil.setStatusBarLightMode(getActivity().getWindow());

    }

}
